var grid;
var opeForm;

$(function() {
	opeForm = $('#opeForm');
	//$('input[type="button"]').button();
	grid = $('#gridTable').jqGrid({
		caption : 'B/D administrator list',
		datatype : 'local',
		mtype : 'post',
		height : '400',
		autowidth : true,
		colModel : [ {
			name : 'DP_USER_ID',
			label : 'UID',
			sortable:false
		}, {
			name : 'BD_ADMIN_NAME',
			label : 'User Name',
			sortable:false
		}, {
			name : 'DEPARTMENT',
			label : 'Department',
			sortable:false
		}],
		viewrecords : true,
		multiselect : false,
		rownumbers: true,
		rownumWidth: 40,
		rowNum: '-1',	// disable paging
		prmNames : {
			page : 'pageNum', // 表示请求页码的参数名称
			rows : 'pageSize' // 表示请求行数的参数名称
		},
		jsonReader : {
			root : 'rows', // json中代表实际模型数据的入口
			page : 'pageNum', // json中代表当前页码的数据
			total : 'totalPage', // json中代表页码总数的数据
			records : 'totalNum', // json中代表数据行总数的数据
			repeatitems : false
		}
	});
	
	queryOperation();
	$('#queryBtn').click(queryOperation);
	$('#clearBtn').click(clearOperation);
	$('#addBtn').click(addOperation);
	$('#modifyBtn').click(editOperation);
	$('#deleteBtn').click(deleteOperation);
	$('#viewBtn').click(viewOperation);
	
});

function queryOperation() {
	var params = opeForm.serializeObject();
	grid.jqGrid('setGridParam', {
		url : 'BD_ADMIN_MANAGE!queryList.do',
		datatype : 'json',
		postData : jQuery.extend({}, params)
	}).trigger('reloadGrid');
}

function clearOperation() {
	opeForm.resetForm();
}

function viewOperation() {
	var id = $('#gridTable').jqGrid('getGridParam', 'selrow');
	if (id == null) {
		alert('Please select a record!');
		return;
	}
	
	var row = $('#gridTable').jqGrid('getRowData', id);
	var url = 'BD_ADMIN_MANAGE!showInfo.do?model.DP_USER_ID=' + row.DP_USER_ID;
	openWin(url, 'pop');
}

function addOperation() {
	var url = 'BD_ADMIN_MANAGE!showAdd.do';
    var title = 'Add B/D admin';
    openWin(url, title);
}


//perform a Update operation
function editOperation() {

	var id = $('#gridTable').jqGrid('getGridParam', 'selrow');
	if (id == null) {
		alert('Please select a record!');
		return;
	}
	
	var row = $('#gridTable').jqGrid('getRowData', id);
	var url = 'BD_ADMIN_MANAGE!showEdit.do?model.DP_USER_ID=' + row.DP_USER_ID;
	openWin(url,'pop');
}

// perform a DELETE operation
function deleteOperation() {

	var id = $('#gridTable').jqGrid('getGridParam', 'selrow');
	if (id == null) {
		alert('Please select a record!');
		return;
	}
	
	var row = $('#gridTable').jqGrid('getRowData', id);
	var params = {'model.DP_USER_ID' : row.DP_USER_ID};
	if (confirm('Are you confirm to delete selected record?')) {
		$.ajax({
			url : 'BD_ADMIN_MANAGE!doDelete.do',
			data : params,
			type : 'post',
			success : function(data) {
				alert('Operation successfull!');
				afterSave();
			}
		});
	}
}

function afterSave() {
	queryOperation();
}
